package com.zz.admin.roleManagement.service.impl;

import java.util.List;
import java.util.ArrayList;
import com.zz.admin.roleManagement.dto.RoleDto;
import com.zz.admin.roleManagement.entity.Role;
import com.zz.admin.roleManagement.mapper.RoleMapper;
import com.zz.admin.roleManagement.service.RoleService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import com.zz.common.base.dto.PageListResultDto;
import com.zz.common.base.param.QueryParam;
import com.zz.common.base.enums.BaseExceptionEnum;
import com.zz.common.constants.BaseConstant;
import com.zz.common.utils.mapper.BeanMapper;
import com.zz.common.utils.common.UUIDGenerator;
import com.zz.common.exception.ExceptionFactory;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


/**
 * 角色表 服务实现类
 * @author zhang.lei
 * @since 2025-04-12 15:31:53
 */
@Service
@Slf4j
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Override
    public PageListResultDto<RoleDto> queryWithPage(RoleDto roleDto, QueryParam queryParam) {
        Page<Role> page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize());
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("del_flag", BaseConstant.NO);
        List<Role> roles = roleMapper.selectList(page, wrapper);
        List<RoleDto> roleDtos = BeanMapper.mapList(roles, Role.class, RoleDto.class);
        return new PageListResultDto<>((int) page.getCurrent(), (int) page.getTotal(), queryParam.getPageSize(), roleDtos);
    }

    @Override
    public List<RoleDto> queryWithNoPage(RoleDto roleDto) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("del_flag", BaseConstant.NO);
        List<Role> roles = roleMapper.selectList(wrapper);
        return BeanMapper.mapList(roles, Role.class, RoleDto.class);
    }

    @Override
    public RoleDto queryById(String id) {
        if (StringUtils.isBlank(id)) {
            return null;
        }
        Role role = roleMapper.selectById(id);
        if (role == null || BaseConstant.YES.equals(role.getDelFlag())) {
            return null;
        }
        return BeanMapper.map(role, RoleDto.class);
    }

    @Override
    public List<RoleDto> queryByIds(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return new ArrayList<>();
        }
        List<Role> roles = roleMapper.selectByIds(ids);
        if (CollectionUtils.isEmpty(roles)) {
            return new ArrayList<>();
        }
        return BeanMapper.mapList(roles, Role.class, RoleDto.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public RoleDto save(RoleDto roleDto) {
        // 根据业务修改
        return insert(roleDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<RoleDto> saveBatch(List<RoleDto> roleDtos) {
        // 根据业务修改
        return insertList(roleDtos);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public RoleDto modify(RoleDto roleDto) {
        // 根据业务修改
        updateById(roleDto);
        return roleDto;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<RoleDto> modifyBatch(List<RoleDto> roleDtos) {
        // 根据业务修改
        return updateList(roleDtos);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        // 根据业务修改
        RoleDto roleDto = new RoleDto();
        roleDto.setRoleId(id);
        deleteById(roleDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteBatch(List<String> ids) {
        // 根据业务修改
        deleteByPrimaryKeys(ids);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public RoleDto insert(RoleDto roleDto) {
        roleDto.setRoleId(UUIDGenerator.generateUUID());
        roleDto.setDelFlag(BaseConstant.NO);
        Role entity = BeanMapper.map(roleDto, Role. class);
        roleMapper.insert(entity);
        return roleDto;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public RoleDto insertWithId(RoleDto roleDto) {
        if (StringUtils.isBlank(roleDto.getRoleId())) {
            roleDto.setRoleId(UUIDGenerator.generateUUID());
        }
        roleDto.setDelFlag(BaseConstant.NO);
        Role entity = BeanMapper.map(roleDto, Role.class);
        roleMapper.insert(entity);
        return roleDto;
    }

    private List<RoleDto> insertList(List<RoleDto> roleDtos) {
        if (CollectionUtils.isEmpty(roleDtos)) {
            throw ExceptionFactory.createBaseException(BaseExceptionEnum.INTERNAL_SERVER_ERROR, "批量插入时，参数不能为空");
        }
        List<Role> insertList = new ArrayList<>();
        for (RoleDto roleDto : roleDtos) {
            if (StringUtils.isBlank(roleDto.getRoleId())) {
                roleDto.setRoleId(UUIDGenerator.generateUUID());
            }
            roleDto.setDelFlag(BaseConstant.NO);
            insertList.add(BeanMapper.map(roleDto, Role.class));
        }
        roleMapper.insert(insertList, 1000);
        return roleDtos;
    }

    private List<RoleDto> updateList(List<RoleDto> roleDtos) {
        if (CollectionUtils.isEmpty(roleDtos)) {
            throw ExceptionFactory.createBaseException(BaseExceptionEnum.INTERNAL_SERVER_ERROR, "批量更新时，参数不能为空");
        }
        for (RoleDto roleDto : roleDtos) {
            if (StringUtils.isBlank(roleDto.getRoleId())) {
                throw ExceptionFactory.createBaseException(BaseExceptionEnum.INTERNAL_SERVER_ERROR, "批量更新时，主键不能为空");
            }
        }
        List<Role> roleList = BeanMapper.mapList(roleDtos, RoleDto.class, Role.class);
        roleMapper.updateById(roleList, 1000);
        return roleDtos;
    }

    @Transactional(rollbackFor = Exception.class)
    public int updateById(RoleDto roleDto) {
        if (StringUtils.isBlank(roleDto.getRoleId())) {
            throw ExceptionFactory.createBaseException(BaseExceptionEnum.INTERNAL_SERVER_ERROR, "更新时，主键不能为空");
        }
        Role entity = BeanMapper.map(roleDto, Role. class);
        return roleMapper.updateById(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    public int trueDeleteById(String id) {
        return roleMapper.deleteById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public int deleteById(RoleDto roleDto) {
        roleDto.setDelFlag(BaseConstant.YES);
        return updateById(roleDto);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteByPrimaryKeys(List<String> ids) {
        List<RoleDto> roleDtos = new ArrayList<>();
        for (String id : ids) {
            RoleDto roleDto = new RoleDto();
            roleDto.setRoleId(id);
            roleDto.setDelFlag(BaseConstant.YES);
            roleDtos.add(roleDto);
        }
        updateList(roleDtos);
    }

}
